<?php

function gravatar($email, $size = 80, $rating = 'g', $default = null, $path = null, $cacheLifetime = 86400)
{
  $gravatarClient = new GravatarClient($email, $size, $rating);
  $gravatarClient->setCacheLifetime($cacheLifetime);

  if ($default)
  {
    $gravatarClient->seDefault($default);
  }

  if (!$path || !file_exists($path))
  {
    $path = sfConfig::get('sf_upload_dir').'/gravatars';
    
    $gravatarClient->setCacheDir($path);
  }

  $gravatar = $gravatarClient->get();

  if ('http://' !== substr($gravatar, 0, 7))
  {
    $gravatar = '/uploads/gravatars/'.$gravatar;
  }

  return image_tag($gravatar, array('alt' => ''));
}